'use strict';

module.exports = (req, res, rfun)=>{
    const DB = require("../mysql");
    let dv = {s:1,m:"error",u:""};
    if(req.POST && req.POST.jsoncs) req.POST = JSON.parse(req.POST.jsoncs);
    delete res.DATA;
    //console.log(req.POST);

    switch(req.GET.pathname){
        case "/Login/UserLogin":{ //用户登录
            if(req.POST.phoneCode != req.SESSION.sms){
                dv.m = "验证码错误";
                break;
            }
            delete req.SESSION.sms;
            var sql = "SELECT A.account,A.checktoken,A.password,B.usrid,B.nickname,B.chips,B.bankchips FROM account A INNER JOIN usr B ON A.account = B.openid WHERE A.account = ? AND moneyconsume = 900000000000000";
            DB.query(sql, function(rs){
                if(rs[0]){
                    let ck = rs[0].checktoken.split("/");
                    let tm = ck[1].split(" ");
                    let np = req.PUBFUN.MD5(ck[0] + req.POST.loginPwd + tm[0])
                    if(rs[0].password == np){
                        dv.u = "/Home/Index";
                        dv.m = "登录成功";
                        dv.s = 0;
                        req.SESSION.accounts = rs[0].account;
                        req.SESSION.userid = rs[0].usrid;
                        req.SESSION.nickname = rs[0].nickname;
                        req.SESSION.gameid = rs[0].usrid;
                        req.SESSION.score = rs[0].chips;
                        req.SESSION.insurescore = rs[0].bankchips;
                        req.SESSION.bankpwd = "";
                        for(let k in SESSION){
                            if(req.SESSION == SESSION[k]) continue;
                            if(SESSION[k].userid == req.SESSION.userid) delete SESSION[k];
                        }
                    }else{
                        dv.m = "账号密码错误";
                        LOG.error(req.POST.loginPwd,req.PUBFUN.MD5(req.POST.loginPwd),np);
                    }
                }else{
                    //dv.m += rs.code[rs.code.length-1];
                    dv.m = "账号或密码错误";
                }
                rfun(dv);
            },[req.POST.userName]);
            return;
        }case "/Login/LoginOut":{ //退出登录
            req.SESSION.userid = 0;
            res.setHeader("location","/Login/Index");
            res.statusCode = 302;
            res.end();
            rfun(false);
            return;
        }case "/Home/SearchNickNameByGameID":{ //根据gameid获取玩家信息
            //console.log(req.headers);
            DB.query('SELECT usrid,nickname FROM usr where usrid = ?', (row)=>{
                if(row.length > 0){
                    dv.s = 0;
                    dv.m = row[0].nickname;
                }else{
                    dv.m = "没有找到该玩家！";
                }
                rfun(dv);
            }, [req.POST.gid])
            return;
        }case "/Login/LoginSendSMS":{ //发送短信到指定手机号
            let mcode = (Math.random()*999999|0).toString();
            if(mcode.length < 6) mcode = '000000'.substr(0,6-mcode.length) + mcode;
            req.SESSION.sms = mcode;
            DB.query("SELECT phone FROM usr WHERE openid = ?", (rs)=>{
                if(rs[0] && rs[0].phone.length == 11){
                    sendCode(rs[0].phone, mcode);
                    dv.m = "发送成功";
                }else{
                    dv.m = "手机号错误";
                }
                dv.s = 0;
                req.LOG.error(rs[0].phone, mcode);
                rfun(dv);
            }, [req.POST.userName]);
            return;
        }case "/Home/DealerSaveScore":{ //存游戏币
            if(!(req.POST.score > 0)){
                dv.m = "游戏币必须大于0";
                rfun(dv);
                return;
            }
            DB.query("SELECT online FROM usr WHERE usrid = ?", (rs)=>{
                if(rs[0].online == 1){
                    dv.m = "客户端在线不能操作";
                    rfun(dv);
                    return;
                }
                DB.query(`UPDATE usr SET chips = chips - ?, bankchips = bankchips + ? WHERE usrid = ? AND chips - ? >= 0`, (row)=>{
                    console.log(row);
                    if(row.affectedRows > 0){
                        dv.m = "存款成功";
                        req.SESSION.score += req.POST.score;
                        req.SESSION.insurescore -= req.POST.score;
                        dv.u = "/Home/Index";
                        dv.s = 0;
                        //添加日志
                        DB.query("INSERT INTO banklog (usrid,chips,times) VALUES (?,?,?)",null,[req.SESSION.userid,req.POST.score,parseInt(Date.now()/1000)])
                    }else{
                        dv.m = "游戏币不够";
                    }
                    rfun(dv);
                }, [req.POST.score,req.POST.score,req.SESSION.userid,req.POST.score]);
            },[req.SESSION.userid])
            return;
        }case "/Home/DealerTakeScore":{ //取游戏币
            DB.query(`UPDATE usr SET chips = chips + ?, bankchips = bankchips - ? WHERE usrid = ? AND bankchips - ? >= 0 AND bankpassword = ?`, (row)=>{
                if(row.affectedRows > 0){
                    dv.m = "取款成功";
                    req.SESSION.score += req.POST.score;
                    req.SESSION.insurescore -= req.POST.score;
                    dv.u = "/Home/Index";
                    dv.s = 0;
                    //添加日志
                    DB.query("INSERT INTO banklog (usrid,chips,times) VALUES (?,?,?)",null,[req.SESSION.userid,-req.POST.score,parseInt(Date.now()/1000)])
                }else{
                    dv.m = "银行密码错误或游戏币不够";
                }
                rfun(dv);
            }, [req.POST.score, req.POST.score, req.SESSION.userid, req.POST.score, req.POST.pwd]);
            return;
        }case "/Home/DealerTransScore":{ //转账
            req.POST.score = req.POST.score|0;
            if(req.POST.score <= 0){
                dv.m = "出错了!!!";
                return rfun(dv);
            }
            DB.query(`SELECT forbiddentime FROM usr WHERE usrid = ?`, (row2,err1)=>{
                if(row2[0].forbiddentime > Date.now()/1000){
                    dv.m = "账号已冻结，无法操作";
                    rfun(dv);
                    return;
                }
                DB.query("SELECT usrid,nickname,chips,bankchips,bankpassword,moneyconsume FROM usr WHERE usrid = ? OR usrid = ?",(rs)=>{
                    let snick,rnick,rchips,rvip = 0;
                    for(let i=0;i<rs.length;i++){
                        if(rs[i].usrid == req.SESSION.userid){
                            req.SESSION.score = rs[i].chips;
                            req.SESSION.insurescore = rs[i].bankchips;
                            snick = rs[i].nickname;
                            if(rs[i].bankpassword != req.POST.pwd){
                                dv.m = "银行密码不正确或者输入有误";
                                return rfun(dv);
                            }
                        }else{
                            rnick = rs[i].nickname;
                            rchips = rs[i].chips;
                            rvip = (rs[i].moneyconsume == 900000000000000?0:1)
                        }
                    }
                    //if([41424,41433].indexOf(req.SESSION.userid) != -1){
                        rvip = 1;
                    //}
                    if(req.SESSION.score < req.POST.score){
                        dv.m = "银行金币不足";
                        return rfun(dv);
                    }
                    DB.query("UPDATE usr SET chips = chips - ? WHERE usrid = ? AND chips - ? > 0;",(rs3)=>{
                        if(rs3.affectedRows > 0){
                            DB.query("INSERT INTO chipstrade(seller,sellername,sellerchips,buyer,buyername,buyerchips,chips,tradetime,send,stat) VALUES(?,?,?,?,?,?,?,NOW(),1,?);",(rs1)=>{
                                DB.query("INSERT INTO mail(id,sendername,senderid,receiverid,title,content,attachmenttype,attachmentnum,isread,mailtype,isgetattachment,relevanceid,lasttime) SELECT (SELECT MAX(id)+1 FROM mail),?,?,?,'筹码赠送',?,1,?,0,0,0,?,?",(rs2)=>{
                                    if(rs2.affectedRows > 0){
                                        dv.m = "转账成功";
                                        req.SESSION.score -= req.POST.score;
                                        req.SESSION.bankpwd = req.POST.pwd;
                                        dv.s = 0;
                                        dv.reobj = {sNickName:req.SESSION.nickname,sGameID:req.SESSION.gameid,tNickName:rnick,tGameID:req.POST.gid,Score:req.POST.score,TranTime:new Date().toLocaleString()};
                                    }else{
                                        dv.m = "出错了";
                                    }
                                    rfun(dv);                          
                                },[snick,req.SESSION.userid,req.POST.gid,'玩家'+snick+' (ID:'+req.SESSION.userid+') 赠送您'+req.POST.score+'筹码!',req.POST.score,rs1.insertId,parseInt(Date.now()/1000+86400*7)])
                            },[req.SESSION.userid,snick,req.SESSION.score,req.POST.gid,rnick,rchips,req.POST.score,rvip])

                        }else{
                            dv.m = "出错了!";
                            rfun(dv); 
                        }
                    },[req.POST.score,req.SESSION.userid,req.POST.score])
                },[req.SESSION.userid,req.POST.gid])
            },[req.SESSION.userid]);
            return;
        }case "/Home/GetTopEmailCount":{
            dv.s = 0;
            break;
        }case "/Record/GetTranRecordList":{ //转出记录
            req.POST.pageIndex = req.POST.pageIndex | 0;
            let sql = DB.format(`SELECT C.id,B.tradetime CollectDate,A.nickname NickName,A.usrid GameID,B.chips SwapScore,CASE WHEN C.id IS NULL OR C.isgetattachment=1 THEN 9 ELSE stat END Status,moneyconsume VIP FROM usr A 
            INNER JOIN chipstrade B ON A.usrid = B.buyer
            LEFT JOIN mail C ON B.id=C.relevanceid AND C.attachmentnum > 0
            WHERE B.seller = ? AND B.tradetime >= ? AND B.tradetime < DATE_ADD(?,INTERVAL 1 DAY) AND (B.buyer = ? OR ? = 0)`, [req.SESSION.userid,req.POST.stime,req.POST.etime,req.POST.gameid,req.POST.gameid]);
            sql = req.PUBFUN.MYPage(sql, "ORDER BY B.tradetime DESC", req.POST.pageIndex, 20, ",SUM(SwapScore) SwapScore,SUM(CASE WHEN VIP <> 900000000000000 THEN SwapScore ELSE 0 END) uScore")
            DB.query(sql, (rs)=>{
                dv = {"list":{"rows":rs[0],"total":rs[1][0].total},"s":0,"m":"加载成功","u":"","total":[{"SwapScore":Number(rs[1][0].SwapScore)}],"usertotal":[{"SwapScore":Number(rs[1][0].uScore)}]};
                dv.list = JSON.stringify(dv.list);
                dv.total = JSON.stringify(dv.total);
                dv.usertotal = JSON.stringify(dv.usertotal);
                //console.log(rs[1],res.DATA, dv);
                rfun(dv);
            });
            return;
        }case "/Record/DealerTranRecord":{ //取消转出
            DB.query("SELECT attachmentnum,isgetattachment,relevanceid,B.stat FROM mail A LEFT JOIN chipstrade B ON A.relevanceid = B.id WHERE senderid = ? AND attachmenttype = 1 AND A.id = ? AND relevanceid > 0",(rs)=>{
                if(rs.length < 1){
                    dv.m = "邮件不存在";
                    return rfun(dv);
                }
                if(rs[0].isgetattachment == 1){
                    dv.m = "邮件已经被领取";
                    return rfun(dv);
                }
                if(rs[0].stat == 2){
                    dv.m = "交易异常";
                    return rfun(dv);
                }
                DB.query("DELETE FROM mail WHERE senderid = ? AND attachmenttype = 1 AND id = ?",(rs1)=>{
                    if(rs1.affectedRows > 0){
                        DB.query("UPDATE usr SET chips = chips + ? WHERE usrid = ?;DELETE FROM chipstrade WHERE seller = ? AND id = ?",(rs2)=>{
                            if(rs2.affectedRows > 0){
                                req.SESSION.score += rs[0].attachmentnum;
                            }
                        },[rs[0].attachmentnum,req.SESSION.userid,req.SESSION.userid,rs[0].relevanceid]);
                        dv.s = 0;
                        return rfun(dv);
                    }
                },[req.SESSION.userid,req.POST.id])
            },[req.SESSION.userid,req.POST.id])
            return;
        }case "/Record/GetTransInRecordList":{ //转入记录
            req.POST.pageIndex = req.POST.pageIndex | 0;
            let sql = DB.format(`SELECT C.id,tradetime CollectDate,sellername NickName,seller GameID,B.chips SwapScore,CASE WHEN C.id IS NULL OR C.isgetattachment=1 THEN 9 ELSE stat END Status,moneyconsume VIP FROM usr A 
            INNER JOIN chipstrade B ON A.usrid=B.seller 
            LEFT JOIN mail C ON B.id = C.relevanceid AND C.attachmentnum > 0
            WHERE B.buyer = ? AND tradetime >= ? AND tradetime < DATE_ADD(?,INTERVAL 1 DAY) AND (B.seller = ? OR ? = 0)`, [req.SESSION.userid,req.POST.stime,req.POST.etime,req.POST.gameid,req.POST.gameid]);
            sql = req.PUBFUN.MYPage(sql, "ORDER BY tradetime DESC", req.POST.pageIndex, 20, ",SUM(SwapScore) SwapScore,SUM(CASE WHEN VIP <> 900000000000000 THEN SwapScore ELSE 0 END) uScore")
            DB.query(sql, (rs)=>{
                dv = {"list":{"rows":rs[0],"total":rs[1][0].total},"s":0,"m":"加载成功","u":"","total":[{"SwapScore":Number(rs[1][0].SwapScore)}],"usertotal":[{"SwapScore":Number(rs[1][0].uScore)}]};
                dv.list = JSON.stringify(dv.list);
                dv.total = JSON.stringify(dv.total);
                dv.usertotal = JSON.stringify(dv.usertotal);
                rfun(dv);
            });
            return;
        }case "/Record/DealerTransInRecord":{ //领取转入
            DB.query("SELECT attachmentnum,isgetattachment,relevanceid FROM mail WHERE receiverid = ? AND attachmenttype = 1 AND id = ? AND relevanceid > 0",(rs)=>{
                if(rs.length < 1){
                    dv.m = "邮件不存在";
                    return rfun(dv);
                }
                if(rs[0].isgetattachment == 1){
                    dv.m = "邮件已经被领取";
                    return rfun(dv);
                }
                DB.query("DELETE FROM mail WHERE receiverid = ? AND attachmenttype = 1 AND id = ?",(rs1)=>{
                    if(rs1.affectedRows > 0){
                        DB.query("UPDATE usr SET chips = chips + ? WHERE usrid = ?",(rs2)=>{
                            if(rs2.affectedRows > 0){
                                req.SESSION.score += rs[0].attachmentnum;
                            }
                        },[rs[0].attachmentnum,req.SESSION.userid]);
                        dv.s = 0;
                        return rfun(dv);
                    }
                },[req.SESSION.userid,req.POST.id])
            },[req.SESSION.userid,req.POST.id])
            return;
        }case "/Record/GetRebateRecordList":{ //返利记录
            req.POST.pageIndex = req.POST.pageIndex | 0;
            //req.PUBFUN.MSPage("SELECT CONVERT(varchar(32),WriteTime,120) InsertDate,CASE WHEN RecordType=51 THEN '单笔1.8%奖励' WHEN RecordType=50 THEN '日销奖励' WHEN RecordType=49 THEN '朋友圈奖励' ELSE '净销奖励' END TName,CASE WHEN addInsureScore>0 THEN addInsureScore ELSE CONVERT(numeric(18,2),addIntegral/100.0) END Score,CASE WHEN addInsureScore>0 THEN '金币' ELSE '红包' END TypeName FROM [GameDB].[dbo].[GameScoreInfoLog] WITH(NOLOCK) where RecordType IN(52,50,51,49) AND WriteTime >= @btime AND WriteTime < DATEADD(DAY,1,@etime) AND UserID=@UserID", "ORDER BY InsertDate DESC", (rs)=>{
            let sql = DB.format("SELECT rid RecordID,Status,times InsertDate,CASE WHEN tid=11 THEN '单笔1.8%奖励' WHEN tid=29 THEN '净销奖励' ELSE '日销奖励' END TName,Score,'金币' TypeName FROM rebateRecord WHERE times >= ? AND times < DATE_ADD(?,INTERVAL 1 DAY) AND usrid = ?", [req.POST.stime,req.POST.etime,req.SESSION.userid]);
            sql = req.PUBFUN.MYPage(sql, "ORDER BY times DESC", req.POST.pageIndex, 20, ",SUM(Score) tScore");
            DB.query(sql, (rs)=>{
                //console.log(rs[1]);
                dv = {"list":{"rows":rs[0],"total":rs[1][0].total},"s":0,"m":"加载成功","u":"","total":[{"GrantScore":rs[1][0].tScore}]};
                dv.list = JSON.stringify(dv.list);
                dv.total = JSON.stringify(dv.total);
                rfun(dv);
            });          
            return;
        }case "/Record/DealerGetRebate":{ //领取返利
            DB.query(`SELECT * FROM rebateRecord WHERE usrid = ? AND rid = ? AND Status = 0`, (rs1)=>{
                if(rs1.length < 1){
                    dv.s = 0;
                    dv.m = "领取过了";
                    dv.u = "/Record/RebateRecord";
                    return rfun(dv);
                }
                console.log(rs1[0]);
                DB.query(`UPDATE rebateRecord SET Status=1 WHERE usrid = ? AND rid = ? AND Status = 0;
                UPDATE usr SET chips = chips + ? WHERE usrid = ?`, (rs)=>{
                    if(rs[0].affectedRows > 0){
                        req.SESSION.score += rs1[0].score;
                    }
                    dv.s = 0;
                    dv.m = "领取成功";
                    dv.u = "/Record/RebateRecord";
                    rfun(dv);
                },[req.SESSION.userid, req.POST.rid, rs1[0].score, req.SESSION.userid]);
            },[req.SESSION.userid, req.POST.rid]);
            return;
        }case "/Total/GetTotalDataList":{ //日统计 周统计 月统计
            let sql = `SELECT CAST(tradetime AS DATE) InsertDate,SUM(CASE WHEN seller=? THEN A.chips ELSE 0 END) TransTotal,SUM(CASE WHEN buyer=? THEN A.chips ELSE 0 END) ReceiveTotal,IFNULL(RebateTotal,0) RebateTotal 
            FROM chipstrade A 
            INNER JOIN usr C ON A.seller = C.usrid
            INNER JOIN usr D ON A.buyer = D.usrid
            LEFT JOIN (SELECT CAST(times AS DATE) InsertDate,SUM(Score) RebateTotal FROM rebateRecord
                WHERE times >= ? AND times < DATE_ADD(?,INTERVAL 2 DAY) AND usrid = ? GROUP BY CAST(times AS DATE)) B ON DATEDIFF(B.InsertDate,A.tradetime) = 1
            LEFT JOIN mail E ON A.id = E.relevanceid AND E.attachmentnum > 0
            WHERE ((A.seller = ? AND D.moneyconsume <> 900000000000000) OR (A.buyer = ?  AND C.moneyconsume <> 900000000000000)) AND (E.isgetattachment=1 OR E.isgetattachment is NULL) AND A.tradetime >= ? AND A.tradetime < DATE_ADD(?,INTERVAL 1 DAY)
            GROUP BY CAST(tradetime AS DATE),B.RebateTotal ORDER BY CAST(tradetime AS DATE) DESC`;
            if(req.POST.type == "week"){
                //sql = `SELECT CONVERT(varchar(10),MIN(WriteTime),120) InsertDate,DATEPART(WEEK,WriteTime) WeekID,SUM(CASE WHEN SUserID=@UserID THEN Score ELSE 0 END) TransTotal,SUM(CASE WHEN TUserID=@UserID THEN Score ELSE 0 END) ReceiveTotal,0 RebateTotal FROM [GameDB].[dbo].[RTransferV] WITH(NOLOCK) WHERE (SUserID=@UserID OR TUserID=@UserID) AND Status=1 AND WriteTime >= @btime AND WriteTime < DATEADD(DAY,1,@etime) GROUP BY DATEPART(WEEK,WriteTime) ORDER BY DATEPART(WEEK,WriteTime) DESC`;
                sql = `SELECT CAST(MIN(tradetime) AS DATE) InsertDate,WEEK(tradetime) WeekID,SUM(CASE WHEN seller=? THEN A.chips ELSE 0 END) TransTotal,SUM(CASE WHEN buyer=? THEN A.chips ELSE 0 END) ReceiveTotal,ISNULL(RebateTotal,0) RebateTotal  
                FROM chipstrade A
                INNER JOIN usr C ON A.seller = C.usrid
                INNER JOIN usr D ON A.buyer = D.usrid
                INNER JOIN (SELECT WEEK(DATE_ADD(times,INTERVAL 1 DAY)) InsertDate,SUM(Score) RebateTotal FROM rebateRecord 
                    WHERE times >= ? AND times < DATE_ADD(?,INTERVAL 2 DAY) AND usrid = ? GROUP BY WEEK(DATEADD(times,INTERVAL 1 DAY))) B ON B.InsertDate = WEEK(tradetime)
                    LEFT JOIN mail E ON A.id = E.relevanceid AND E.attachmentnum > 0
                WHERE ((A.seller = ? AND D.moneyconsume <> 900000000000000) OR (A.buyer = ?  AND C.moneyconsume <> 900000000000000)) AND (E.isgetattachment=1 OR E.isgetattachment is NULL) AND A.tradetime >= ? AND A.tradetime < DATE_ADD(?,INTERVAL 1 DAY) 
                GROUP BY WEEK(tradetime),B.RebateTotal ORDER BY WEEK(tradetime) DESC`;
            }else if(req.POST.type == "month"){
                req.POST.stime += "-01";
                req.POST.etime += "-01";
                sql = `SELECT DATE_FORMAT(tradetime, '%Y-%m') InsertDate,SUM(CASE WHEN seller=? THEN A.chips ELSE 0 END) TransTotal,SUM(CASE WHEN buyer=? THEN A.chips ELSE 0 END) ReceiveTotal,IFNULL(RebateTotal,0) RebateTotal 
                FROM chipstrade A
                INNER JOIN usr C ON A.seller = C.usrid
                INNER JOIN usr D ON A.buyer = D.usrid
                INNER JOIN (SELECT DATE_FORMAT(DATE_ADD(times,INTERVAL -1 DAY),'%Y-%m') InsertDate,SUM(Score) RebateTotal FROM rebateRecord 
                    WHERE times >= ? AND times < DATE_ADD(?,INTERVAL 1 MONTH) AND usrid=? GROUP BY DATE_FORMAT(DATE_ADD(times,INTERVAL -1 DAY),'%Y-%m')) B ON B.InsertDate = DATE_FORMAT(tradetime, '%Y-%m')
                LEFT JOIN mail E ON A.id = E.relevanceid AND E.attachmentnum > 0
                WHERE ((A.seller = ? AND D.moneyconsume <> 900000000000000) OR (A.buyer = ?  AND C.moneyconsume <> 900000000000000)) AND (E.isgetattachment=1 OR E.isgetattachment is NULL) AND A.tradetime >= ? AND A.tradetime < DATE_ADD(?,INTERVAL 1 MONTH) 
                GROUP BY DATE_FORMAT(tradetime, '%Y-%m'),B.RebateTotal ORDER BY DATE_FORMAT(tradetime, '%Y-%m') DESC`;
            }
            DB.query(sql, (row)=>{
                dv = {"list":{"rows":row,"total":row.length},"s":0,"m":"加载成功","u":"","total":[{"TransTotal":0,"ReceiveTotal":0,"RebateTotal":0}]};
                dv.list = JSON.stringify(dv.list);
                for(let v of row){
                    dv.total[0].TransTotal += Number(v.TransTotal);
                    dv.total[0].ReceiveTotal += Number(v.ReceiveTotal);
                    dv.total[0].RebateTotal += Number(v.RebateTotal);
                }
                dv.total = JSON.stringify(dv.total);
                rfun(dv);
            },[req.SESSION.userid, req.SESSION.userid, req.POST.stime, req.POST.etime, req.SESSION.userid, req.SESSION.userid, req.SESSION.userid, req.POST.stime, req.POST.etime],true)
            return;
        }
    }
    rfun(dv);
}

//发送手机验证码 网易易盾
function sendCode(mob, code){
    const https = require("https"), game = require("../../config/game"), fun = require("../pubfun");
    let op = {host:"sms.dun.163yun.com",port:443,path:"/v2/sendsms","method": "POST",
        headers:{
        "Content-Type":"application/x-www-form-urlencoded;charset=utf-8",
        "Content-Length":0
    }};
    let pd = {
        "businessId":game.sms.businessId,
        "mobile":mob,
        //"needUp":"true",
        "nonce":fun.MD5(Date.now()+"hong"),
        "params":"code="+code,
        "secretId":game.sms.secretId,
        "templateId":game.sms.templateId,
        "timestamp":Date.now().toString(),
        "version":"v2",
    }, pdstr = "", mdstr = "";
    for(let k in pd){
        //pdstr += k + "=" + (k=="params"?encodeURIComponent(pd[k]):pd[k]) + "&";
        pdstr += k + "=" + pd[k] + "&";
        mdstr += k + pd[k];
    }
    pdstr += "signature=" + fun.MD5(mdstr + game.sms.SECRETKEY); //console.log(pdstr, mdstr);
    op.headers["Content-Length"] = pdstr.length;
    https.request(op, (res)=>{
        let cd = Buffer.allocUnsafe(0);
        res.once("data", (chunk)=>{
            cd = Buffer.concat([cd, chunk], cd.length + chunk.length);
        });
        res.once("end", ()=>{
            LOG.info(mob, cd.toString());
        });
    }).end(pdstr);
}